package business;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import business.vo.BookingVO;
import model.BookingDTO;
import model.FacilityDTO;
import model.UserDTO;
import dao.DAOException;
import dao.DAOFactory;
import dao.FacilityDAO;
import dao.BookingDAO;
import dao.UserDAO;
import model.FacilitySearchbyTypeDTO;
import model.AvailableDTO;

public class BookingManager {

	private FacilityDAO facilityDAO;
	private BookingDAO bookingDAO;
	private UserDAO userDAO;

	public BookingManager() {
		bookingDAO = DAOFactory.loadInstance().getBookingDAO();
		facilityDAO = DAOFactory.loadInstance().getFacilityDAO();
		userDAO = DAOFactory.loadInstance().getUserDAO();
	}

	@SuppressWarnings("finally")
	public ArrayList<FacilityDTO> findfacilityByCriteria(
			FacilitySearchbyTypeDTO facility) {
		ArrayList<FacilityDTO> currentList = new ArrayList<FacilityDTO>();
		try {
			currentList = facilityDAO.searchAvailableFacilityByType(facility);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return currentList;
		}
	}

	@SuppressWarnings("finally")
	public ArrayList<BookingVO> findCurrentBooking() {
		ArrayList<BookingDTO> currentList = new ArrayList<BookingDTO>();
		ArrayList<BookingVO> bkVO = new ArrayList<BookingVO>();
		try {
			currentList = bookingDAO.findCurrentBooking();
			for (BookingDTO each : currentList) {
				BookingVO bookingVO = new BookingVO();

				UserDTO user = userDAO.findUser(each.getUserID());
				bookingVO.setUserName(user.getUserName());

				FacilityDTO fdto = facilityDAO.findFacility(each
						.getFacilityID());
				bookingVO.setFacilityName(fdto.getFacilityName());

				bookingVO.setStartDate(each.getStartDate());
				bookingVO.setPeriod(each.getPeriod());
				bookingVO.setStatus(each.getStatus());
				bookingVO.setBookingID(each.getBookingID());
				bkVO.add(bookingVO);
			}
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return bkVO;
		}
	}

	public ArrayList<BookingVO> findBookingByUserAndStatus(UserDTO user) {

		ArrayList<BookingVO> bv = new ArrayList<BookingVO>();

		try {
			ArrayList<BookingDTO> bookingUser = new ArrayList<BookingDTO>();
			bookingUser = bookingDAO.findBookingByUserAndStatus(user);

			for (BookingDTO bd : bookingUser) {
				BookingVO bkv = new BookingVO();
				bkv.setBookingID(bd.getBookingID());
				int facID = bd.getFacilityID();
				FacilityDTO fac = facilityDAO.findFacility(facID);
				bkv.setFacilityName(fac.getFacilityName());
				bkv.setStartDate(bd.getStartDate());
				bkv.setPeriod(bd.getPeriod());
				bkv.setStatus(bd.getStatus());
				bv.add(bkv);
			}
		} catch (DAOException e) {
			e.printStackTrace();
		} finally {
			return bv;
		}
	}

	@SuppressWarnings("finally")
	public ArrayList<BookingVO> findAllBookingByUser(UserDTO user) {

		ArrayList<BookingVO> bv = new ArrayList<BookingVO>();

		try {
			ArrayList<BookingDTO> bookingUser = new ArrayList<BookingDTO>();
			bookingUser = bookingDAO.findAllBookingByUser(user);

			for (BookingDTO bd : bookingUser) {
				BookingVO bkv = new BookingVO();
				bkv.setBookingID(bd.getBookingID());
				bkv.setUserID(bd.getUserID());
				int facID = bd.getFacilityID();
				FacilityDTO fac = facilityDAO.findFacility(facID);
				bkv.setFacilityName(fac.getFacilityName());
				bkv.setStartDate(bd.getStartDate());
				bkv.setPeriod(bd.getPeriod());
				bkv.setStatus(bd.getStatus());
				bv.add(bkv);
			}
		} catch (DAOException e) {
			e.printStackTrace();
		} finally {
			return bv;
		}
	}

@SuppressWarnings("finally")
	public ArrayList<BookingVO> findBookingbyStatus(int managerID) {

		ArrayList<BookingVO> bk = new ArrayList<BookingVO>();

		try {
			ArrayList<BookingDTO> bking = bookingDAO
					.FindBookingByStatus(managerID);

			for (BookingDTO dto : bking) {
				BookingVO bvo = new BookingVO();

				bvo.setBookingID(dto.getBookingID());
				int userid = dto.getUserID();
				UserDTO user = userDAO.findUser(userid);
				bvo.setUserName(user.getUserName());
				bvo.setUserID(userid);
				
				int faciID=dto.getFacilityID();
				FacilityDTO faci=facilityDAO.findFacility(faciID);
				bvo.setFacilityName(faci.getFacilityName());

				bvo.setStartDate(dto.getStartDate());
				bvo.setPeriod(dto.getPeriod());
				bvo.setStatus(dto.getStatus());
				bvo.setPriority(dto.getPriority());
				bvo.setReason(dto.getReason());
				bk.add(bvo);
			}

		} catch (DAOException e) {
			e.printStackTrace();
		}

		finally {
			return bk;
		}
	}

	public void approveReject(BookingDTO booking) {
		try {
			bookingDAO.approveReject(booking);
		} catch (DAOException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<FacilityDTO> findfacilityByCriteria(AvailableDTO booking) {
		ArrayList<FacilityDTO> currentList = new ArrayList<FacilityDTO>();
		try {
			currentList = facilityDAO.availableFacility(booking);
		} catch (DAOException e) {
			e.printStackTrace();
		} finally {
			return currentList;
		}
	}

	public void addBooking(BookingDTO booking) {
		try {
			bookingDAO.insertBooking(booking);
		} catch (DAOException e) {
			e.printStackTrace();
		}
	}

	public void updateBooking(BookingDTO booking) {
		try {
			bookingDAO.updateBooking(booking);
		} catch (DAOException e) {
			e.printStackTrace();
		}
	}

	public void deleteBooking(BookingDTO bookDel) {
		try {
			bookingDAO.deleteBooking(bookDel);
		} catch (DAOException e) {
			e.printStackTrace();
		}
	}

	public void cancelBooking(BookingDTO bookcancel) {
		try {
			bookingDAO.cancelBooking(bookcancel);
		} catch (DAOException e) {
			e.printStackTrace();
		}
	}

	public BookingDTO getBooking(int bookID) {
		BookingDTO book = new BookingDTO();
		try {
			book = bookingDAO.getBooking(bookID);
		} catch (DAOException e) {
			e.printStackTrace();
		} finally {
			return book;
		}
	}

	public BookingVO findBookingDetails(int bookID) {
		BookingVO bookingVO = new BookingVO();
		BookingDTO booking;
		UserDTO user;
		FacilityDTO facility;
		try {
			booking = bookingDAO.getBooking(bookID);
			bookingVO.setBookingID(booking.getBookingID());
			bookingVO.setStartDate(booking.getStartDate());
			bookingVO.setPeriod(booking.getPeriod());
			bookingVO.setStatus(booking.getStatus());
			bookingVO.setReason(booking.getReason());
			bookingVO.setComment(booking.getComment());
			bookingVO.setPriority(booking.getPriority());
			Calendar cal = Calendar.getInstance();
			cal.setTime(bookingVO.getStartDate());
			bookingVO.setEndDate(new Date(cal.getTimeInMillis()
					+ bookingVO.getPeriod() * 86400000));
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			user = userDAO.findUser(bookingDAO.getBooking(bookID).getUserID());
			bookingVO.setUserID(user.getUserID());
			bookingVO.setUserName(user.getUserName());
		} catch (DAOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			facility = facilityDAO.findFacility(bookingDAO.getBooking(bookID)
					.getFacilityID());
			bookingVO.setFacilityID(facility.getFacilityID());
			bookingVO.setFacilityName(facility.getFacilityName());
			bookingVO.setFacilityType(facility.getFacilityType());
			bookingVO.setCapacity(facility.getCapacity());
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// book = bookingVO.
		return bookingVO;

	}

	public ArrayList<BookingDTO> findBookingbyFacilityID(int facilityID) {
		ArrayList<BookingDTO> currentList = new ArrayList<BookingDTO>();
		try {
			currentList = bookingDAO.findBookings(facilityID);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return currentList;
		}

	}

}